package com.qkjia.elasticsearchdemo;


import com.alibaba.fastjson.JSON;
import com.qkjia.domain.Goods;
import com.qkjia.mapper.GoodsMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaoqiankun
 * @date 2021/8/30
 */
@SpringBootTest
public class ElasticsearchTest2 {
    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * importorg.junit.jupiter.api.Test;
     */
    @Test
    void contextLoads() {
        System.out.println(client);
    }

    /**
     * 1. 批量操作 bulk
     */
    @Test
    public void testBulk() throws IOException {
        //创建bulkrequest对象，整合所有操作
        BulkRequest bulkRequest = new BulkRequest();

        /*
        # 1. 删除1号记录
        # 2. 添加6号记录
        # 3. 修改3号记录 名称为 “三号”
         */
        //添加对应操作
        //1. 删除1号记录
        DeleteRequest deleteRequest = new DeleteRequest("person", "1");
        bulkRequest.add(deleteRequest);

        //2. 添加6号记录
        Map map = new HashMap();
        map.put("name", "六号");
        IndexRequest indexRequest = new IndexRequest("person").id("6").source(map);
        bulkRequest.add(indexRequest);


        Map map2 = new HashMap();
        map2.put("name", "三号");
        //3. 修改3号记录 名称为 “三号”
        UpdateRequest updateReqeust = new UpdateRequest("person", "3").doc(map2);
        bulkRequest.add(updateReqeust);


        //执行批量操作
        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        // 返回结果状态
        RestStatus status = response.status();
        System.out.println(status);

    }


    /**
     * 批量导入  -- 是报错的
     */
    @Test
    public void importData() throws IOException {
        //1.查询所有数据，mysql
        List<Goods> goodsList = goodsMapper.findAll();

        //System.out.println(goodsList.size());
        //2.bulk导入
        BulkRequest bulkRequest = new BulkRequest();

        //2.1 循环goodsList，创建IndexRequest添加数据
        for (Goods goods : goodsList) {
            //2.2 设置spec规格信息 Map的数据   specStr:{}
            //goods.setSpec(JSON.parseObject(goods.getSpecStr(),Map.class));

            String specStr = goods.getSpecStr();
            //将json格式字符串转为Map集合
            Map map = JSON.parseObject(specStr, Map.class);
            //设置spec map
            goods.setSpec(map);
            //将goods对象转换为json字符串
            String data = JSON.toJSONString(goods);//map --> {}
            IndexRequest indexRequest = new IndexRequest("goods");
            indexRequest.id(goods.getId() + "").source(data, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }


        BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }


    /**
     * 查询所有
     * 1. matchAll
     * 2. 将查询结果封装为Goods对象，装载到List中
     * 3. 分页。默认显示10条
     */
    @Test
    public void testMatchAll() throws IOException {
        //2. 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        //4. 创建查询条件构建器SearchSourceBuilder
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //6. 查询条件
        MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
        //查询所有文档
        //5. 指定查询条件
        sourceBuilder.query(query);
        //3. 添加查询条件构建器 SearchSourceBuilder
        searchRequest.source(sourceBuilder);

        // 8 . 添加分页信息
        sourceBuilder.from(0);
        sourceBuilder.size(100);

        //1. 查询,获取查询结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        //7. 获取命中对象 SearchHits
        SearchHits searchHits = searchResponse.getHits();
        //7.1 获取总记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);


        List<Goods> goodsList = new ArrayList<>();
        //7.2 获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //获取json字符串格式的数据  getSourceAsString: 作为字符串的文档来源（
            String sourceAsString = hit.getSourceAsString();
            //转为java对象
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);

        }


        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }


    /**
     * termQuery:词条查询
     */
    @Test
    public void testTermQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
        // 查询条件
        QueryBuilder query = QueryBuilders.termQuery("title", "华为");//term词条查询
        // 指定查询条件
        sourceBulider.query(query);
        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);

        // 查询结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 获取命中对象 searchHits
        SearchHits searchHits = searchResponse.getHits();
        // 获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        //  获取Hits数据  数组  getHits:搜索请求的点击量
        for (SearchHit hit : hits) {
            // 获取json字符串格式的数据  getSourceAsString: 作为字符串的文档来源（
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }

    /**
     * matchQuery:词条分词查询，分词之后的等值匹配
     */
    @Test
    public void testMatchQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
        // 查询条件
        MatchQueryBuilder query = QueryBuilders.matchQuery("title", "华为手机");
        query.operator(Operator.AND);//求并集
        // 指定查询条件
        sourceBulider.query(query);
        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);

        // 查询结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 获取命中对象 searchHits
        SearchHits searchHits = searchResponse.getHits();
        //获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        //  获取Hits数据  数组  getHits:搜索请求的点击量
        for (SearchHit hit : hits) {
            // 获取json字符串格式的数据  getSourceAsString: 作为字符串的文档来源（
            String sourceAsString = hit.getSourceAsString();
            //转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }

    /**
     * 模糊查询:WildcardQuery
     */
    @Test
    public void testWildcardQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
        // 查询条件    wildcardQuery:实现通配符搜索查询
        WildcardQueryBuilder query = QueryBuilders.wildcardQuery("title", "化*");
        // 指定查询条件
        sourceBulider.query(query);
        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);

        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 获取命中对象 searchHits
        SearchHits searchHits = searchResponse.getHits();
        // 获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            // 获取json字符串格式的数据  getSourceAsString: 作为字符串的文档来源
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }


    /**
     * 1. 范围查询：rangeQuery
     * 2. 排序
     */
    @Test
    public void testRangeQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();

        //范围查询
        RangeQueryBuilder query = QueryBuilders.rangeQuery("price");
        //指定下限 gte大于等于
        query.gte(2000);
        //指定上限 小于等于
        query.lte(3000);

        // 指定查询条件
        sourceBulider.query(query);
        //排序
        sourceBulider.sort("price", SortOrder.DESC);
        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);
        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHits searchHits = searchResponse.getHits();
        //获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //  获取Hits数据  数组  getHits:搜索请求的点击量
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }


    /**
     * queryString
     * # queryString查询-脚本
     * # queryString：
     * # • 会对查询条件进行分词。
     * # • 然后将分词后的查询条件和词条进行等值匹配
     * # • 默认取并集（OR）
     * # • 可以指定多个查询字段
     */
    @Test
    public void testQueryStringQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();

        //queryString
        QueryStringQueryBuilder query = QueryBuilders.queryStringQuery("华为手机")
                .field("title")
                .field("categoryName")
                .field("brandName")
                .defaultOperator(Operator.AND);//求并集
        // 指定查询条件
        sourceBulider.query(query);

        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);
        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHits searchHits = searchResponse.getHits();
        //获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //  获取Hits数据  数组  getHits:搜索请求的点击量
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }


    /**
     * 布尔查询：boolQuery
     * 1. 查询品牌名称为:华为
     * 2. 查询标题包含：手机
     * 3. 查询价格在：2000-3000
     */
    @Test
    public void testBoolQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
        // 1.构建boolQuery
        BoolQueryBuilder query = QueryBuilders.boolQuery();

        // 2.构建各个查询条件
        // 2.1 查询品牌名称为:华为
        QueryBuilder termQuery = QueryBuilders.termQuery("brandName", "华为");
        query.must(termQuery);

        // 2.2. 查询标题包含：手机
        QueryBuilder matchQuery = QueryBuilders.matchQuery("title", "手机");
        query.filter(matchQuery);

        // 2.3 查询价格在：2000-3000
        QueryBuilder rangeQuery = QueryBuilders.rangeQuery("price");
        ((RangeQueryBuilder) rangeQuery).gte(2000);
        ((RangeQueryBuilder) rangeQuery).lte(3000);
        query.filter(rangeQuery);

        // 3.使用boolQuery连接
        // 指定查询条件
        sourceBulider.query(query);
        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);
        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHits searchHits = searchResponse.getHits();
        //获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //  获取Hits数据  数组  getHits:搜索请求的点击量
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }


    /**
     * 聚合查询：桶聚合，分组查询
     * 1. 查询title包含手机的数据
     * 2. 查询品牌列表
     */
    @Test
    public void testAggQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();

        // 1. 查询title包含手机的数据
        MatchQueryBuilder query = QueryBuilders.matchQuery("title", "手机");
        // 指定查询条件
        sourceBulider.query(query);

        // 2. 查询品牌列表 聚合
        /*
        参数：
            1. 自定义的名称，将来用于获取数据
            2. 分组的字段
         */
        AggregationBuilder agg = AggregationBuilders.terms("goods_brands").field("brandName").size(100);
        sourceBulider.aggregation(agg);

        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);
        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHits searchHits = searchResponse.getHits();
        //获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数：" + value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //  获取Hits数据  数组  getHits:搜索请求的点击量
            String sourceAsString = hit.getSourceAsString();
            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }

        // 获取聚合结果
        Aggregations aggregations = searchResponse.getAggregations();

        // 返回的Aggregation ■通过聚合名称作为关键字。
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        //System.out.println(aggregationMap);
        Terms goods_brands = (Terms) aggregationMap.get("goods_brands");

        // 返回此术语聚合中的桶的排序列表。
        List<? extends Terms.Bucket> buckets = goods_brands.getBuckets();

        List brands = new ArrayList();
        for (Terms.Bucket bucket : buckets) {
            Object key = bucket.getKey();
            brands.add(key);
        }

        for (Object brand : brands) {
            System.out.println(brand);
        }

    }

    /**
     *
     * 高亮查询：
     *  1. 设置高亮
     *      * 高亮字段
     *      * 前缀
     *      * 后缀
     *  2. 将高亮了的字段数据，替换原有数据
     */
    @Test
    public void testHighLightQuery() throws IOException {

        // 构建查询请求对象，指定查询的索引名称
        SearchRequest searchRequest = new SearchRequest("goods");
        // 创建查询条件构造器SearchSourceBuilder
        SearchSourceBuilder sourceBulider = new SearchSourceBuilder();

        // 1. 查询title包含手机的数据
        MatchQueryBuilder query = QueryBuilders.matchQuery("title", "手机");
        // 指定查询条件
        sourceBulider.query(query);

        // 设置高亮
        HighlightBuilder highlighter = new HighlightBuilder();
        // 设置三要素
        highlighter.field("title");
        highlighter.preTags("<font color='red'>");
        highlighter.postTags("</font>");

        // 添加突出显示以作为搜索的一部分执行。
        sourceBulider.highlighter(highlighter);


        // 2. 查询品牌列表
        /*
        参数：
            1. 自定义的名称，将来用于获取数据
            2. 分组的字段
         */
        AggregationBuilder agg = AggregationBuilders.terms("goods_brands").field("brandName").size(100);
        sourceBulider.aggregation(agg);

        // 添加查询条件构造器     SearchSourceBuilder
        searchRequest.source(sourceBulider);

        // 结果
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //  获取Hits数据  数组  getHits:搜索请求的点击量
        SearchHits searchHits = searchResponse.getHits();
        // 获取记录数
        long value = searchHits.getTotalHits().value;
        System.out.println("总记录数："+value);

        List<Goods> goodsList = new ArrayList<>();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //  获取Hits数据  数组  getHits:搜索请求的点击量
            String sourceAsString = hit.getSourceAsString();

            // 转为java
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);

            // 获取高亮结果，替换 goods 中的 title
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField HighlightField = highlightFields.get("title");
            //  HighlightField.fragments();返回的Aggregation ■通过聚合名称作为关键字。
            Text[] fragments = HighlightField.fragments();
            // 替换
            goods.setTitle(fragments[0].toString());


            goodsList.add(goods);
        }

        for (Goods goods : goodsList) {
            System.out.println(goods);
        }


    }
}



